<?php
/**
 * Created by PhpStorm.
 * User: liiyu
 * Date: 2018/6/7
 * Time: 1:51
 */

namespace app\api\controller\v17;

use app\api\controller\v2\Auth;
use app\common\cachemodel\Orders as OrdersCacheModel;
use app\common\cachemodel\OrdersLib as OrdersCacheModelLib;
use Exception;
use think\response\Json;

/**
 * @version 2.0.0
 * 每日运势订单接口
 * Class Orders
 * @package app\api\controller\v2
 */
class Orders extends Auth
{
    /**
     * 接口，订单列表
     * @return Json
     * @throws Exception
     */
    public function index()
    {
        $this->msg = '';
        OrdersCacheModelLib::getUserAllUnpaidOrder($this->user['id']);
        $type = input('param.type/d', 0);
        $order_type = input('param.order_type/s', '');
        $version_code = input('param.version_code/d', 0);

        $ordersModel = new OrdersCacheModel();
        //1.9.6版本以后不需要过滤本命预测
        if ($version_code)
        {
            switch (true)
            {
                case $this->headers['app-type'] === 'ios' && $version_code >= 22:
                case $this->headers['app-type'] === 'android' && $version_code >= 34:
                    $orders = $ordersModel->getUserAllOrder($this->user['id'], $type, $order_type, '', true);
                    break;
                default:
                    $orders = $ordersModel->getUserAllOrder($this->user['id'], $type, $order_type, '7', true);
                    break;
            }
        } else
        {
            $orders = $ordersModel->getUserAllOrder($this->user['id'], $type, $order_type, '7', true);
        }
        if ($orders)
        {
            $orders['data'] = array_values($this->resetOrderList($orders['ids'], $orders['data']));
            unset($orders['ids']);
        }
        return show(config('code.success'), '加载成功', $orders);
    }

    /**
     * 订单列表数据重构
     * @param $order_ids
     * @param $orders
     * @return array
     * @throws Exception
     */
    public function resetOrderList($order_ids, $orders)
    {
        $data = [];
        $ordersModel = new OrdersCacheModel();
        foreach ($order_ids as $order_id)
        {
            $orders[$order_id] = $ordersModel->ordersDetail($orders[$order_id]);
            $data[$order_id]['order_type'] = $orders[$order_id]['order_type'];
            $data[$order_id]['order_id'] = $orders[$order_id]['order_id'];
            $data[$order_id]['trade_type'] = $orders[$order_id]['trade_type'];
            $data[$order_id]['total_amount'] = $orders[$order_id]['total_amount'];
            $data[$order_id]['create_time'] = date('Y-m-d H:i:s', $orders[$order_id]['create_time']);
            $data[$order_id]['order_btns'] = $ordersModel->getIndexOrderBtn($orders[$order_id]);
            $data[$order_id]['order_status'] = $orders[$order_id]['status_des'];
            $data[$order_id]['mini_type_des'] = $orders[$order_id]['mini_type_des'];
            $order_data['des1'] = '';
            $order_data['des2'] = '';

            //修改小程序下单的支付方式
            if ($orders[$order_id]['trade_type'] == 13 || $orders[$order_id]['trade_type'] == 8 || $orders[$order_id]['trade_type'] == 15)//微信
            {
                $data[$order_id]['trade_type'] = 2;
            } elseif ($orders[$order_id]['trade_type'] == 7)//支付宝
            {
                $data[$order_id]['trade_type'] = 1;
            }

            switch (true)
            {
                //重组校准生辰订单
                case (int)$orders[$order_id]['order_type'] === 1:
                    $rec = $ordersModel->RecRead($orders[$order_id]);
                    $data[$order_id]['order_name'] = '校准生辰';
                    $order_data['title'] = '校准生辰服务';
                    $order_data['des1'] = '出生时间：' . date('Y-m-d', $rec['data']['start_time']);
                    $order_data['des2'] = '时间范围：' . date('H:i', $rec['data']['start_time']) . '-' . ((date('d', $rec['data']['start_time']) != date('d', $rec['data']['end_time'])) ? '次日' : '') . date('H:i', $rec['data']['end_time']);
                    break;
                //重组运势订单
                case in_array((int)$orders[$order_id]['order_type'], [2, 3, 4, 12], true):
                    $fortune = $ordersModel->FortuneRead($orders[$order_id]);
                    switch ($orders[$order_id]['order_type'])
                    {
                        case 2:
                            $order_data['title'] = '每日运势';
                            break;
                        case 3:
                            $order_data['title'] = '月度运势';
                            break;
                        case 4:
                            $order_data['title'] = '年度运势';
                            break;
                        default :
                            $order_data['title'] = '';
                            break;
                    }
                    $data[$order_id]['order_name'] = '运势订单';
                    $recode_name = [];
                    foreach ($fortune['recode_data'] as $recode)
                    {
                        $recode_name[] = $recode['name'];
                        $order_data['des2'] = '开通日期：' . $fortune['fortune_data']['start_time'] . '至' . $fortune['fortune_data']['end_time'];
                    }
                    $frame = $recode_name ? implode(',', $recode_name) : '';
                    $order_data['des1'] = '开通档案：' . $frame;
                    break;
                //重组地图订单
                case (int)$orders[$order_id]['order_type'] === 5:
                    $map = $ordersModel->MapRead($orders[$order_id]);
                    $data[$order_id]['order_name'] = '幸运之城';
                    $order_data['title'] = '幸运之城解锁';
                    $map_recode = $map['recode_data'] ? current($map['recode_data']) : [];
                    $game = [];
                    foreach ($map['goods_data'] as $acg)
                    {
                        $game[] = $acg['name'];
                    }
                    $map_count = count($game);
                    $gabe_sta = $game ? implode(',', $game) : '';
                    if ($map_count > 2)
                    {
                        $des = '开通' . implode(',', array_slice($game, 0, 2)) . '等' . $map_count . '条';
                    } else
                    {
                        $des = '开通' . $gabe_sta . '等' . $map_count . '条';
                    }
                    $order_data['des1'] = '购买档案：' . $map_recode['name'];
                    $order_data['des2'] = '订单详情：' . $des;
                    $data[$order_id]['order_data'][] = [
                        'name' => '订单详情',
                        'value' => $gabe_sta
                    ];
                    break;
                //重组本命预测订单
                case (int)$orders[$order_id]['order_type'] === 7:
                    $report = $ordersModel->MapRead($orders[$order_id]);
                    $data[$order_id]['order_name'] = '一生报告订单';
                    $order_data['title'] = '一生报告解锁';
                    $game = [];
                    foreach ($report['goods_data'] as $acg)
                    {
                        $game[] = $acg['name'];
                    }
                    $map_count = count($game);
                    $gabe_sta = $game ? implode(',', $game) : '';
                    if ($map_count > 1)
                    {
                        $des = '开通' . implode(',', array_slice($game, 0, 1)) . '等' . $map_count . '篇';
                    } else
                    {
                        $des = '开通' . $gabe_sta;
                    }
                    $order_data['des1'] = $des;
                    $data[$order_id]['order_data'][] = [
                        'name' => '订单详情',
                        'value' => $gabe_sta
                    ];
                    break;
                //重组视频订单
                case (int)$orders[$order_id]['order_type'] === 6:
                    $video = $ordersModel->VideoRead($orders[$order_id]);
                    $video_goods = $video['goods_data'] ? current($video['goods_data']) : [];
                    $data[$order_id]['order_name'] = '教学视频';
                    $data[$order_id]['video_id'] = $video['video_id'];
                    $order_data['title'] = $video_goods['name'];
                    $order_data['des1'] = $video['goods_data_des'];
                    break;
                //重组vip订单
                case (int)$orders[$order_id]['order_type'] === 8:
                    $vip = $ordersModel->VipRead($orders[$order_id]);
                    $vip_goods = $vip['goods_data'] ? current($vip['goods_data']) : [];
                    $data[$order_id]['order_name'] = '开通会员';
                    $order_data['title'] = '会员开通';
                    $order_data['des1'] = '开通时长：' . $vip_goods['name'];
                    //若会员购买成功后，显示有效期
                    if ((int)$orders[$order_id]['status'] === 4)
                    {
                        $order_data['des2'] = '有效期至：' . $vip['vip_end_time'];
                    }
                    break;
            }
            if (isset($order_data) && $order_data)
            {
                $data[$order_id]['data'][] = $order_data;
            }
        }
        return $data;
    }
}
